#region license
# This file is part of Grawlix.
# 
# Grawlix is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 3 of
# the License, or (at your option) any later version.
# 
# Grawlix is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public
# License along with Grawlix.  If not, see
# <http://www.gnu.org/licenses/>.
#endregion

"""Base class for main client-side user code."""

namespace Grawlix.Application
import Grawlix
import Grawlix.Application.Gui
import Grawlix.Services
import Grawlix.Services.Rendering
import Grawlix.Services.Resources

abstract transient class UIGovernor(GovernorBase, IRenderableArea):
"""The object that interfaces with the user."""
	
	protected def constructor([required] game as Game):
	"""
	Initializes governor services.
	Param game: The game this governor belongs to.
	"""
		super(game)
		_guiManager = GuiManager()
		_textureFactory = \
			game.FindService[of IResourceFactory[of ITextureResource]]()
	
	virtual def Render(device as IRenderDevice, camera as Rect):
	"""
	Render the specified area, in screen coordinates.
	Param device: The device to use to render the specified area.
	Param camera: The coordinates in which to render.
	Remarks: All state must be consistent during a render.
		Be careful if overriding.
	"""
		# In case of exception, set to 0 first
		elapsed = _renderElapsed
		_renderElapsed = timespan.Zero
		
		_textureFactory.Update(elapsed)
		_guiManager.Render(device, camera)
	
	override def Update(elapsed as timespan):
	"""
	Take care of per-frame updates.
	Param elasped: The time since the last update.
	"""
		_renderElapsed += elapsed
		_guiManager.Update(elapsed)
	
	override def Run(message as Message):
	"""
	Do default message handling.
	Param message: The given message.
	"""
		(self as duck).RunDefault(message)
	
	def RunDefault(message as KeyDownInputMessage):
	"""
	Send an OnKeyDown to the GUI.
	Param message: The given message.
	"""
		control = _guiManager.KeyboardFocus
		if control is not null:
			control.OnKeyDown(message.Code)
	
	def RunDefault(message as KeyUpInputMessage):
	"""
	Send an OnKeyUp to the GUI.
	Param message: The given message.
	"""
		control = _guiManager.KeyboardFocus
		if control is not null:
			control.OnKeyUp(message.Code)
	
	def RunDefault(message as CharacterInputMessage):
	"""
	Send an OnCharacter to the GUI.
	Param message: The given message.
	"""
		control = _guiManager.KeyboardFocus
		if control != null:
			control.OnCharacter(message.Character)
	
	def RunDefault(message as MouseMoveInputMessage):
	"""
	Send an OnMouseMove to the GUI.
	Param message: The given message.
	"""
		position = message.Position
		area as Rect
		control = _guiManager.FindMouseFocus(position, area)
		if control is not null:
			delta = \
				_guiManager.Viewport.ProjectDisplacement(message.Delta, area)
			control.OnMouseMove(position, delta, area)
	
	def RunDefault(message as MouseWheelInputMessage):
	"""
	Send an OnMouseWheel to the GUI.
	Param message: The given message.
	"""
		position = message.Position
		area as Rect
		control = _guiManager.FindMouseFocus(position, area)
		if control is not null:
			control.OnMouseWheel(position, message.Ticks, area)
	
	def RunDefault(message as MouseDownInputMessage):
	"""
	Send an OnMouseDown to the GUI.
	Param message: The given message.
	"""
		position = message.Position
		area as Rect
		control = _guiManager.FindMouseFocus(position, area)
		if control is not null:
			control.OnMouseDown(position, message.Code, area)
	
	def RunDefault(message as MouseUpInputMessage):
	"""
	Send an OnMouseUp to the GUI.
	Param message: The given message.
	"""
		position = message.Position
		area as Rect
		control = _guiManager.FindMouseFocus(position, area)
		if control is not null:
			control.OnMouseUp(position, message.Code,area)
	
	[getter(GuiManager)]
	private _guiManager as GuiManager
	"""The GuiManager for this governor."""
	
	[getter(TextureFactory)]
	private _textureFactory as IResourceFactory[of ITextureResource]
	"""The texture factory used in this domain."""
	
	private _renderElapsed as timespan
	"""The time since a render occurred."""
